package com.zz.framework.core.base
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.zz.framework.R
import kotlin.properties.Delegates

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created by zanezhao on 2020/3/1 10:08 PM.
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
class FragmentParentActivity : BaseImmersionBarActivity() {
    companion object {
        val KEY_FRAGMENT = "KEY_FRAGMENT"
        val KEY_TOOLBAR = "KEY_TOOLBAR"
    }

    var fragment: Fragment by Delegates.notNull()
    var baseFragment: BaseImmersionBarFragment by Delegates.notNull()

    override fun onInitAttribute(ba: BaseAttribute) {
        super.onInitAttribute(ba)
        ba.mContentLayoutId = R.layout.activity_with_toolbar_layout
        val mHasTitle = intent.getBooleanExtra(KEY_TOOLBAR, false)
        val clz = intent.getSerializableExtra(KEY_FRAGMENT) as Class<*>
        val cls = clz.name
        fragment = Fragment.instantiate(this, cls)
        if (fragment is BaseImmersionBarFragment) baseFragment = fragment as BaseImmersionBarFragment
        val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
        ba.mHasTitle=mHasTitle
        ft.replace(R.id.mContentLayout, fragment, cls)
        ft.commitAllowingStateLoss()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        baseFragment?.onActivityResult(requestCode, resultCode, data)
    }

    override fun onBackPressed() {
        if (baseFragment != null && baseFragment.onBackPressed()) {
            super.onBackPressed()
        }
    }

    override fun initView() {

    }

    override fun initData() {

    }
}